import axios from 'axios';
export default {
	isApp: true,
	currentCityCode: 'PVG',
	UserIdlist: [],
	Refreshlist: [],
	loginpath: '',
	getTime: function(start) {
		let _this = this;
		let i = 1000 * 60 * 5;
		if (start) {
			console.info(_this.hadStartGetTime);
			if (_this.hadStartGetTime) {
				console.info(window.localStorage['datetime']);
				return window.localStorage['datetime'];
			}
			if (_this.isFirstGetTime) {
				axios({
					method: 'post',
					url: 'Test/GetTime',
				})
					.then(function(res) {
						_this.isFirstGetTime = false;
						console.info(res.data.msg);
						window.localStorage['datetime'] = res.data.msg;
						return res.data.msg;
					})
					.catch(function(err) {
						console.info(err);
						_this.isFirstGetTime = false;
						let date = window.localStorage['datetime'];
						if (date != null && date != undefined && date != '') {
							date = date.substring(0, 19);
							date = date.replace(/-/g, '/');
							let timestamp = new Date(date).getTime();
							timestamp = timestamp + i;
							let time1 = new Date(timestamp);
							console.info(time1);
							return _this.DateTimeFormat(
								time1,
								'yyyy-MM-dd hh:mm:ss'
							);
						} else {
							let time1 = new Date();
							console.info(time1);
							return _this.DateTimeFormat(
								time1,
								'yyyy-MM-dd hh:mm:ss'
							);
						}
					});
			}
			setInterval(function() {
				axios({
					method: 'post',
					url: 'Test/GetTime',
				})
					.then(function(res) {
						_this.isFirstGetTime = false;
						_this.hadStartGetTime = true;
						console.info(res.data.msg);
						window.localStorage['datetime'] = res.data.msg;
						return res.data.msg;
					})
					.catch(function(err) {
						console.info(err);
						_this.isFirstGetTime = false;
						let date = window.localStorage['datetime'];
						if (date != null && date != undefined && date != '') {
							date = date.substring(0, 19);
							date = date.replace(/-/g, '/');
							let timestamp = new Date(date).getTime();
							timestamp = timestamp + i;
							let time1 = new Date(timestamp);
							console.info(time1);
							return _this.DateTimeFormat(
								time1,
								'yyyy-MM-dd hh:mm:ss'
							);
						} else {
							let time1 = new Date();
							console.info(time1);
							return _this.DateTimeFormat(
								time1,
								'yyyy-MM-dd hh:mm:ss'
							);
						}
					});
			}, i);
		} else {
			let date = window.localStorage['datetime'];
			let time2 = new Date();
			if (date != null && date != undefined && date != '') {
				let date2 = date.replace(/-/g, '/');
				let time1 = new Date(date2);
				if (time2 > time1 /*&& time2 - time1 < i*/) {
					date = _this.DateTimeFormat(time2, 'yyyy-MM-dd hh:mm:ss');
					window.localStorage['datetime'] = date;
				}
				console.info(date);
				return date;
			} else {
				console.info(time2);
				return _this.DateTimeFormat(time2, 'yyyy-MM-dd hh:mm:ss');
			}
		}
	},
	DateTimeFormat: function(date, fmt) {
		var o = {
			'M+': date.getMonth() + 1, //月份
			'd+': date.getDate(), //日
			'h+': date.getHours(), //小时
			'm+': date.getMinutes(), //分
			's+': date.getSeconds(), //秒
			'q+': Math.floor((date.getMonth() + 3) / 3), //季度
			S: date.getMilliseconds(), //毫秒
		};
		if (/(y+)/.test(fmt))
			fmt = fmt.replace(
				RegExp.$1,
				(date.getFullYear() + '').substr(4 - RegExp.$1.length)
			);
		for (var k in o) {
			if (new RegExp('(' + k + ')').test(fmt)) {
				fmt = fmt.replace(
					RegExp.$1,
					RegExp.$1.length == 1
						? o[k]
						: ('00' + o[k]).substr(('' + o[k]).length)
				);
			}
		}
		console.info(fmt);
		return fmt;
	},
	//打开数据库，生成数据表
	OpenIndexdb: function() {
		var request = window.indexedDB.open('YtoDB', 3);
		request.onerror = function(event) {
			console.info(event);
			alert('数据库打开报错');
		};
		console.log('数据库打开成功');
		request.onupgradeneeded = function(event) {
			console.log('数据表创建成功！');
			var db = event.target.result;
			var homedelivery = db.createObjectStore('home-delivery', {
				keyPath: 'Tagid',
			});
			var airportdepoperate = db.createObjectStore(
				'airport-dep-operate',
				{ keyPath: 'Tagid' }
			);
			var airportarrivaloperate = db.createObjectStore(
				'airport-arrival-operate',
				{ keyPath: 'Tagid' }
			);
			var customersignfor = db.createObjectStore('customer-signfor', {
				keyPath: 'Tagid',
			});
			homedelivery.createIndex('Tagid', 'Tagid', { unique: false });
			airportdepoperate.createIndex('Tagid', 'Tagid', { unique: false });
			airportarrivaloperate.createIndex('Tagid', 'Tagid', {
				unique: false,
			});
			customersignfor.createIndex('Tagid', 'Tagid', { unique: false });
		};
	},
	//清除表数据
	ClearStore: function(tabname) {
		var table = '';
		switch (tabname) {
			case 'whole':
				table = 'whole';
				break;
			case 'home-delivery':
				table = 'home-delivery';
				break;
			case 'airport-dep-operate':
				table = 'airport-dep-operate';
				break;
			case 'airport-arrival-operate':
				table = 'airport-arrival-operate';
				break;
			case 'customer-signfor':
				table = 'customer-signfor';
				break;
			default:
				console.error(
					'Table Name: ' + tabname + ' is NULL or undefined'
				);
				return;
		}
		var request = window.indexedDB.open('YtoDB', 3);
		request.onerror = function(event) {
			console.info(event);
			alert('数据库打开报错');
		};
		request.onsuccess = function(event) {
			console.info(event);
			var db = request.result;
			if (table == 'whole') {
				db.transaction(['home-delivery'], 'readwrite')
					.objectStore('home-delivery')
					.clear();
				db.transaction(['airport-dep-operate'], 'readwrite')
					.objectStore('airport-dep-operate')
					.clear();
				db.transaction(['airport-arrival-operate'], 'readwrite')
					.objectStore('airport-arrival-operate')
					.clear();
				db.transaction(['customer-signfor'], 'readwrite')
					.objectStore('customer-signfor')
					.clear();
			} else {
				db.transaction([table], 'readwrite')
					.objectStore(table)
					.clear();
			}
		};
	},
};
